/*
 * Copyright © 2016-2019 WAWSCM Inc. All rights reserved.
 */

package com.wawscm.service.impl;

import com.wawscm.commons.error.AssertUtils;
import com.wawscm.mapper.UserInfoMapper;
import com.wawscm.model.UserInfo;
import com.wawscm.service.UserService;
import org.springframework.stereotype.Service;

/**
 * @author Wang Chengwei
 * @since 1.0.0
 */
@Service
public class UserServiceImpl implements UserService {

    private UserInfoMapper userInfoMapper;

    /**
     * spring inject
     */
    public UserServiceImpl(UserInfoMapper userInfoMapper) {
        this.userInfoMapper = userInfoMapper;
    }

    @Override
    public void addPoint(String userId, Integer point) {
        AssertUtils.argNotEmpty(userId, "userId");
        AssertUtils.argNotNull(point, "point");
        if (point <= 0) {
            throw new IllegalArgumentException("积分必须大于0");
        }

        UserInfo userInfo = this.userInfoMapper.selectByPrimaryKey(userId);
        AssertUtils.notNull(userInfo, "用户不存在");

        UserInfo temp = new UserInfo();
        temp.setUserId(userId);
        temp.setPoints(userInfo.getPoints() + point);
        this.userInfoMapper.updateByPrimaryKeySelective(temp);
    }
}
